library shell;

include <core.vhs>

import core::*;
import core::Pattern::*;

use java.io.File as File;
use java.util.LinkedList as Queue;
use java.util.Stack as Stack;

function fileExists(filename)
	new File(filename).exists();

function exec(command, outputFile='') 
	Runtime.exec(command, outputFile);

function directoryTravesal(initDir, func, includePattern=null, excludePattern=null) {
	var cur = new File(initDir);
	var queue = new Queue();
	var subs;
	var path;
	// first, visit current folder
	// suppose that the initial direction is always included, regardless to includePattern and excludePatten
	queue.add(cur);
	while (queue.size() > 0) {
		cur = queue.poll();
		func?(cur);
		subs = cur.listFiles();
		for(var f: subs) {
			path = f.getAbsolutePath();
			if (f.isDirectory() && (includePattern == null || isMatch(path, includePattern)) && (excludePattern == null || !isMatch(path, excludePattern)))
				queue.add(f);
		}
	}
}

function directoryDFTravesal(initDir, func, includePattern=null, excludePattern=null) {
	var cur = new File(initDir);
	var stack = new Stack();
	var subs;
	var path;
	// first, visit current folder
	// suppose that the initial direction is always included, regardless to includePattern and excludePatten
	stack.push(cur);
	while (!stack.empty()) {
		cur = stack.pop();
		func?(cur);
		subs = cur.listFiles();
		for(var f: subs) {
			path = f.getAbsolutePath();
			if (f.isDirectory() && (includePattern == null || isMatch(path, includePattern)) && (excludePattern == null || !isMatch(path, excludePattern)))
				stack.push(f);
		}
	}
}